home *** CD-ROM | disk | FTP | other *** search
/ OpenGL Superbible (2nd Edition) / OpenGL SuperBible e2.iso / tools / Mesa-3.0 / SRC / MACROS.H < prev    next >
Encoding:
C/C++ Source or Header  |  1998-04-01  |  6.3 KB  |  259 lines

  1. /* $Id: macros.h,v 3.2 1998/03/27 04:42:06 brianp Exp $ */
  2.  
  3. /*
  4.  * Mesa 3-D graphics library
  5.  * Version:  3.0
  6.  * Copyright (C) 1995-1998  Brian Paul
  7.  *
  8.  * This library is free software; you can redistribute it and/or
  9.  * modify it under the terms of the GNU Library General Public
  10.  * License as published by the Free Software Foundation; either
  11.  * version 2 of the License, or (at your option) any later version.
  12.  *
  13.  * This library is distributed in the hope that it will be useful,
  14.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  16.  * Library General Public License for more details.
  17.  *
  18.  * You should have received a copy of the GNU Library General Public
  19.  * License along with this library; if not, write to the Free
  20.  * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  21.  */
  22.  
  23.  
  24. /*
  25.  * $Log: macros.h,v $
  26.  * Revision 3.2  1998/03/27 04:42:06  brianp
  27.  * added NO_CONST stuff
  28.  *
  29.  * Revision 3.1  1998/03/01 20:16:36  brianp
  30.  * added braces to ASSIGN_*V macros
  31.  *
  32.  * Revision 3.0  1998/01/31 20:58:46  brianp
  33.  * initial rev
  34.  *
  35.  */
  36.  
  37.  
  38. /*
  39.  * A collection of useful macros.
  40.  */
  41.  
  42.  
  43. #ifndef MACROS_H
  44. #define MACROS_H
  45.  
  46.  
  47. #include <math.h>
  48. #include <string.h>
  49.  
  50.  
  51. #ifdef DEBUG
  52. #  include <assert.h>
  53. #  define ASSERT(X)   assert(X)
  54. #else
  55. #  define ASSERT(X)
  56. #endif
  57.  
  58.  
  59. /* Limits: */
  60. #define MAX_GLUSHORT    0xffff
  61. #define MAX_GLUINT    0xffffffff
  62.  
  63.  
  64.  
  65. /* Copy short vectors: */
  66.  
  67. #define COPY_2V( DST, SRC )    DST[0] = SRC[0];    \
  68.                 DST[1] = SRC[1];
  69.  
  70. #define COPY_3V( DST, SRC )    DST[0] = SRC[0];    \
  71.                 DST[1] = SRC[1];    \
  72.                 DST[2] = SRC[2];
  73.  
  74. #define COPY_4V( DST, SRC )    DST[0] = SRC[0];    \
  75.                 DST[1] = SRC[1];    \
  76.                 DST[2] = SRC[2];    \
  77.                 DST[3] = SRC[3];
  78.  
  79. /*
  80.  * Copy a vector of 4 GLubytes from SRC to DST.
  81.  */
  82. #define COPY_4UBV(DST, SRC)            \
  83.    if (sizeof(GLuint)==4*sizeof(GLubyte)) {    \
  84.       *((GLuint*)(DST)) = *((GLuint*)(SRC));    \
  85.    }                        \
  86.    else {                    \
  87.       (DST)[0] = (SRC)[0];            \
  88.       (DST)[1] = (SRC)[1];            \
  89.       (DST)[2] = (SRC)[2];            \
  90.       (DST)[3] = (SRC)[3];            \
  91.    }
  92.  
  93.  
  94.  
  95. /* Assign scalers to short vectors: */
  96. #define ASSIGN_2V( V, V0, V1 )  { V[0] = V0;  V[1] = V1; }
  97.  
  98. #define ASSIGN_3V( V, V0, V1, V2 )  { V[0] = V0;  V[1] = V1;  V[2] = V2; }
  99.  
  100. #define ASSIGN_4V( V, V0, V1, V2, V3 ) { V[0] = V0;    \
  101.                          V[1] = V1;    \
  102.                          V[2] = V2;    \
  103.                                          V[3] = V3; }
  104.  
  105.  
  106. /* Test if we're inside a glBegin / glEnd pair: */
  107. #define INSIDE_BEGIN_END(CTX)  ((CTX)->Primitive!=GL_BITMAP)
  108.  
  109.  
  110.  
  111. /* Absolute value (for Int, Float, Double): */
  112. #define ABSI(X)  ((X) < 0 ? -(X) : (X))
  113. #define ABSF(X)  ((X) < 0.0F ? -(X) : (X))
  114. #define ABSD(X)  ((X) < 0.0 ? -(X) : (X))
  115.  
  116.  
  117.  
  118. /* Round a floating-point value to the nearest integer: */
  119. #define ROUNDF(X)  ( (X)<0.0F ? ((GLint) ((X)-0.5F)) : ((GLint) ((X)+0.5F)) )
  120.  
  121.  
  122. /* Compute ceiling of integer quotient of A divided by B: */
  123. #define CEILING( A, B )  ( (A) % (B) == 0 ? (A)/(B) : (A)/(B)+1 )
  124.  
  125.  
  126. /* Clamp X to [MIN,MAX]: */
  127. #define CLAMP( X, MIN, MAX )  ( (X)<(MIN) ? (MIN) : ((X)>(MAX) ? (MAX) : (X)) )
  128.  
  129.  
  130. /* Min of two values: */
  131. #define MIN2( A, B )   ( (A)<(B) ? (A) : (B) )
  132.  
  133.  
  134. /* MAX of two values: */
  135. #define MAX2( A, B )   ( (A)>(B) ? (A) : (B) )
  136.  
  137.  
  138. /* Dot product of two 3-element vectors */
  139. #define DOT3( a, b )  ( a[0]*b[0] + a[1]*b[1] + a[2]*b[2] )
  140.  
  141.  
  142. /* Dot product of two 4-element vectors */
  143. #define DOT4( a, b )  ( a[0]*b[0] + a[1]*b[1] + a[2]*b[2] + a[3]*b[3] )
  144.  
  145.  
  146.  
  147. /*
  148.  * Integer / float conversion for colors, normals, etc.
  149.  */
  150.  
  151. /* Convert GLubyte in [0,255] to GLfloat in [0.0,1.0] */
  152. #define UBYTE_TO_FLOAT(B)    ((GLfloat) (B) * (1.0F / 255.0F))
  153.  
  154. /* Convert GLfloat in [0.0,1.0] to GLubyte in [0,255] */
  155. #define FLOAT_TO_UBYTE(X)    ((GLubyte) (GLint) (((X)) * 255.0F))
  156.  
  157.  
  158. /* Convert GLbyte in [-128,127] to GLfloat in [-1.0,1.0] */
  159. #define BYTE_TO_FLOAT(B)    ((2.0F * (B) + 1.0F) * (1.0F/255.0F))
  160.  
  161. /* Convert GLfloat in [-1.0,1.0] to GLbyte in [-128,127] */
  162. #define FLOAT_TO_BYTE(X)    ( (((GLint) (255.0F * (X))) - 1) / 2 )
  163.  
  164.  
  165. /* Convert GLushort in [0,65536] to GLfloat in [0.0,1.0] */
  166. #define USHORT_TO_FLOAT(S)    ((GLfloat) (S) * (1.0F / 65535.0F))
  167.  
  168. /* Convert GLfloat in [0.0,1.0] to GLushort in [0,65536] */
  169. #define FLOAT_TO_USHORT(X)    ((GLushort) (GLint) ((X) * 65535.0F))
  170.  
  171.  
  172. /* Convert GLshort in [-32768,32767] to GLfloat in [-1.0,1.0] */
  173. #define SHORT_TO_FLOAT(S)    ((2.0F * (S) + 1.0F) * (1.0F/65535.0F))
  174.  
  175. /* Convert GLfloat in [0.0,1.0] to GLshort in [-32768,32767] */
  176. #define FLOAT_TO_SHORT(X)    ( (((GLint) (65535.0F * (X))) - 1) / 2 )
  177.  
  178.  
  179. /* Convert GLuint in [0,4294967295] to GLfloat in [0.0,1.0] */
  180. #define UINT_TO_FLOAT(U)    ((GLfloat) (U) * (1.0F / 4294967295.0F))
  181.  
  182. /* Convert GLfloat in [0.0,1.0] to GLuint in [0,4294967295] */
  183. #define FLOAT_TO_UINT(X)    ((GLuint) ((X) * 4294967295.0))
  184.  
  185.  
  186. /* Convert GLint in [-2147483648,2147483647] to GLfloat in [-1.0,1.0] */
  187. #define INT_TO_FLOAT(I)        ((2.0F * (I) + 1.0F) * (1.0F/4294967294.0F))
  188.  
  189. /* Convert GLfloat in [-1.0,1.0] to GLint in [-2147483648,2147483647] */
  190. /* causes overflow:
  191. #define FLOAT_TO_INT(X)        ( (((GLint) (4294967294.0F * (X))) - 1) / 2 )
  192. */
  193. /* a close approximation: */
  194. #define FLOAT_TO_INT(X)        ( (GLint) (2147483647.0 * (X)) )
  195.  
  196.  
  197.  
  198. /* Memory copy: */
  199. #ifdef SUNOS4
  200. #define MEMCPY( DST, SRC, BYTES) \
  201.     memcpy( (char *) (DST), (char *) (SRC), (int) (BYTES) )
  202. #else
  203. #define MEMCPY( DST, SRC, BYTES) \
  204.     memcpy( (void *) (DST), (void *) (SRC), (size_t) (BYTES) )
  205. #endif
  206.  
  207.  
  208. /* Memory set: */
  209. #ifdef SUNOS4
  210. #define MEMSET( DST, VAL, N ) \
  211.     memset( (char *) (DST), (int) (VAL), (int) (N) )
  212. #else
  213. #define MEMSET( DST, VAL, N ) \
  214.     memset( (void *) (DST), (int) (VAL), (size_t) (N) )
  215. #endif
  216.  
  217.  
  218. /* MACs and BeOS don't support static larger than 32kb, so... */
  219. #if defined(macintosh) && !defined(__MRC__)
  220.   extern char *AGLAlloc(int size);
  221.   extern void AGLFree(char* ptr);
  222. #  define DEFARRAY(TYPE,NAME,SIZE)  TYPE *NAME = (TYPE*)AGLAlloc(sizeof(TYPE)*(SIZE))
  223. #  define UNDEFARRAY(NAME)          AGLFree((char*)NAME)
  224. #elif defined(__BEOS__)
  225. #  define DEFARRAY(TYPE,NAME,SIZE)  TYPE *NAME = (TYPE*)malloc(sizeof(TYPE)*(SIZE))
  226. #  define UNDEFARRAY(NAME)          free(NAME)
  227. #else
  228. #  define DEFARRAY(TYPE,NAME,SIZE)  TYPE NAME[SIZE]
  229. #  define UNDEFARRAY(NAME)
  230. #endif
  231.  
  232.  
  233. /* Some compilers don't like some of Mesa's const usage */
  234. #ifdef NO_CONST
  235. #  define CONST
  236. #else
  237. #  define CONST const
  238. #endif
  239.  
  240.  
  241.  
  242. /* Pi */
  243. #ifndef M_PI
  244. #define M_PI (3.1415926)
  245. #endif
  246.  
  247.  
  248. /* Degrees to radians conversion: */
  249. #define DEG2RAD (M_PI/180.0)
  250.  
  251.  
  252. #ifndef NULL
  253. #define NULL 0
  254. #endif
  255.  
  256.  
  257.  
  258. #endif /*MACROS_H*/
  259.